package com.itcoon.cloud.framework.utils;/**
 * @author Athric Yu
 * 2024/9/25
 **/

import com.itcoon.cloud.framework.constants.CommonCoreConstants;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 *
 * @author Athric Yu
 * 2024/9/25
 **/
public class FieldUtil {

    public static String getGetterName(Field field) {
        String prefix = field.getType().equals(boolean.class) ? CommonCoreConstants.IS_GETTER_PREFIX : CommonCoreConstants.GETTER_PREFIX;
        return prefix + field.getName().substring(0,1).toUpperCase() + field.getName().substring(1);
    }

    public static String getSetterName(Field Field) {
        String fieldName = Field.getName();
        return CommonCoreConstants.SETTER_PREFIX + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
    }

    public static List<Field> getAllDeclareField(Class<?> clazz){
        Field[] fields = clazz.getDeclaredFields();
        Class<?> superClass = clazz.getSuperclass();
        List<Field> fieldList = new ArrayList<>(Arrays.asList(fields));
        if(superClass != null && !superClass.isInterface() && !superClass.equals(Object.class)){
            List<Field> superFields = getAllDeclareField(superClass);
            fieldList.addAll(superFields);
        }
        return fieldList;
    }

    public static Field getField(Class<?> clazz, String name){
        try {
            return clazz.getField(name);
        } catch (NoSuchFieldException e) {
            return null;
        }
    }
}
